<?php defined('SYSPATH') or die('No direct script access.');
require ('application/classes/dao/base.php');
class Controller_productm extends Controller {


	public function action_index(){
		echo View::factory('manager/index');
	}

	public function action_list(){
		echo View::factory('manager/product');
	}

    public function action_updateStatus(){
        $parm = get($this->request);
        $result = rpcCall("product.updateStatus",$parm);
        echo "success";
    }
    public function action_getList(){
          $parm = get($this->request);
          if(!isset($parm['page']) ||($parm['page'])==""){
            echo "page不能为空";
            exit;
        }
        if(!isset($parm['rows']) ||($parm['rows'])==""){
            echo "rows能为空";
            exit;
        }
        $result = rpcCall("product.getList",$parm);
        echo json_encode($result);
    }
 public function action_getListShelves(){
          $parm = get($this->request);
          if(!isset($parm['page']) ||($parm['page'])==""){
            echo "page不能为空";
            exit;
        }
        if(!isset($parm['rows']) ||($parm['rows'])==""){
            echo "rows能为空";
            exit;
        }
        $result = rpcCall("product.getListShelves",$parm);
        echo json_encode($result);
    }

    public function action_detail(){
        $parm = get($this->request);
         if(!isset($parm['id'])||$parm['id']==""){
            die("产品标题不能为空");
        }
         $result = rpcCall("product.detail",$parm);

         echo View::factory('manager/updateProduct')->bind("bean",$result)->set("action","update")->set("product",$result['data']);

    }
    public function check(){
                $parm = post($this->request);
        echo '<meta http-equiv="content-type" content="text/html;charset=utf-8">';
        if(!isset($parm['title'])||$parm['title']==""){
            die("产品标题不能为空");

        }

        if(!isset($parm['OriginalPrice'])||is_numeric(['OriginalPrice'])){
            die("产品原价各式不正确");

        }
        if(!isset($parm['content1'])||$parm['content1']==""){
            die("产品标题内容为空");

        }
        if(!isset($parm['mainpic'])||count($parm['mainpic'])!=6){
            die("产品图片各式不正确");

        }
        if(!isset($parm['iscoupon'])||$parm['iscoupon']==""){
            die("优惠券不能为空");

        }
        if(!isset($parm['category'])||$parm['category']==""){
            die("分类不能为空");

        }
        if(!isset($parm['classification'])||$parm['classification']==""){
            die("二级分类不能为空");

        }
     
        if(!isset($parm['morethan'])||count($parm['morethan'])==0){
            die("超过多少公斤开始计算费用不能为空");

        }
        if(!isset($parm['price'])||count($parm['price'])==0){
            die("价格不能为空");

        }
        if(!isset($parm['pricedefault'])||($parm['pricedefault'])==""){
            die("默认价格不能为空");

        }

    }
    public function action_addProduct(){
        $parm = post($this->request);
        self::check();
        $log = Log::instance();
        $log->add(Log::ERROR, "用户添加产品", array(':user' => '123',));
        
        // exit;
        $result = rpcCall("product.add",$parm);
        echo "<script>window.location.href='".Url::base()."index.php/productm/list'</script>";
//   $result = rpcCall("category.updateStatus",$parm);
    }

	public function action_action_add(){

        $parm = post($this->request);
        $result = rpcCall("product.productInitData",$parm);
		echo View::factory('manager/addProduct')->bind("product",$result)->set("action","add");
	}
    public function action_updateProduct(){
        $parm = post($this->request);
        if(!isset($parm['openspecification'])||$parm['openspecification']==""){
            die("请选择是否修改规格");


        }
        if(!isset($parm['openexpress'])||$parm['openexpress']==""){
            die("请选择是否修改快递配送模版");


        }
        self::check();

        $result = rpcCall("product.updateProduct",$parm);
        echo "<script>window.location.href='".Url::base()."index.php/productm/list'</script>";
    }
	public function action_upload(){
        $filename = NULL;
        $log = Log::instance();
        if (isset($_FILES['Filedata']['name']))
        {
            $log->add(Log::ERROR, "用户上传图片", array(':user' => '123',));
            $filename = $this->_save_image($_FILES['Filedata']);
            echo json_encode($filename);
            $log->add(Log::ERROR, $filename, array(':user' => '123',));
        }
       // $view->uploaded_file = $filename;
       // $view->error_message = $error_message;
	}

	 protected function _save_image($image)
    {
        if (
            ! Upload::valid($image) OR
            ! Upload::not_empty($image) OR
            ! Upload::type($image, array('jpg', 'jpeg', 'png', 'gif')))
        {
            return FALSE;
        }
 
        $directory = DOCROOT.'temp/';
 
        if ($file = Upload::save($image, NULL, $directory))
        {
            $filename = strtolower(Text::random('alnum', 20)).'.jpg';
 
            Image::factory($file)->save($directory.$filename);
 
            // Delete the temporary file
            unlink($file);
            $msg['status'] = true;
            $msg['filename'] = $filename;
            return $filename;
        }
        $msg['status'] = false;
        return $msg;
    }


 
}
